
#Include <Array.au3>
#Include <Constants.au3>
#Include <File.au3>
#Include <Process.au3>
#Include <String.au3>

Const $cExtensionsC = '*.3gp;*.avi;*.avs;*.divx;*.f4v;*.flv;*.m4v;*.mkv;*.mp3;*.mp4;*.mpg;*.ogm;*.vob'

; Select Video Folder
While 1
	$sFolder = FileSelectFolder( 'Select Video Folder.' , '' , 6 )
	If @Error = 1 AND $sFolder = '' Then
		MsgBox( 4096 , 'Exit:' , 'Ending Application' )
		Exit
	ElseIf $sFolder = '' Then
		MsgBox( 4096 , 'Error:' , 'Unable To Open Folder.' )
	ElseIf NOT FileExists ( $sFolder ) Then
		MsgBox( 4096 , 'Error:' , 'Invalid Folder.' )
	Else
		If StringRight( $sFolder , 1 ) <> '\' Then $sFolder &= '\'
		$aAllFiles = _FileListToArrayXT( $sFolder , $cExtensionsC , 1 , 2 , True , '' , 1 )
		If NOT IsArray( $aAllFiles ) Then
			MsgBox( 4096 , 'Error:' , 'No Sub Folders Found.' )
		Else
			_ArraySort( $aAllFiles , 0 , 1 )
			ExitLoop
		EndIf
	EndIf
WEnd

For $ii = 1 To $aAllFiles[0]
	If StringInStr( $aAllFiles[$ii] , '\Complete\' ) <> 0 Then
		_Macros( $aAllFiles[$ii] )
		FileMove( $aAllFiles[$ii] , $sParent , 9 )
		If DirGetSize( $sParent & 'Complete\' ) = 0 Then DirRemove( $sParent & 'Complete\' )
	EndIf
Next

MsgBox ( 0 , 'Finished' , 'Batch Operation Complete' )

Func _Macros( $sFile )
	Global $sFileDir    = StringLeft     ( $sFile     , StringInStr( $sFile     , '\' , 0 , -1 ) )
	Global $sFileExt    = StringTrimLeft ( $sFile     , StringInStr( $sFile     , '.' , 0 , -1 ) - 1 )
	Global $sFileName   = StringTrimLeft ( $sFile     , StringInStr( $sFile     , '\' , 0 , -1 ) )
	Global $sShortName  = StringLeft     ( $sFileName , StringInStr( $sFileName , '.' , 0 , -1 ) - 1 )
	Global $sParent     = StringLeft     ( $sFileDir  , StringInStr( $sFileDir  , '\' , 0 , -2 ) )
	Global $sFolderName = StringTrimRight( StringTrimLeft( $sFileDir , StringInStr( $sFileDir , '\' , 0 , -2 ) ) , 1 )
EndFunc


; #FUNCTION# ===========================================================================================
; Name:             _FileListToArrayXT
; Description:      Lists files and\or folders in specified path(s) (Similar to using Dir with the /B Switch)
;                   additional features: multi-path, multi-filter, multi-exclude-filter, path format options, recursive search
;                   Corrected on 2010/08/19: Added FileClose()
; Syntax:           _FileListToArrayXT([$sPath = @ScriptDir, [$sFilter = "*", [$iRetItemType, [$bRecursive = False, [$sExclude = "", [$iRetFormat = 1]]]]]])
; Parameter(s):     $sPath = optional: Search path(s), semicolon delimited (default: @ScriptDir)
;                            (Example: "C:\Tmp;D:\Temp")
;                   $sFilter = optional: Search filter(s), semicolon delimited . Wildcards allowed. (default: "*")
;                              (Example: "*.exe;*.txt")
;                   $iRetItemType = Include in search: 0 = Files and Folder, 1 = Files Only, 2 = Folders Only
;                   $iRetPathType = Returned element format: 0 = file/folder name only, 1 = relative path, 2 = full path
;                   $bRecursive = optional: True: recursive search including all subdirectories
;                                           False (default): search only in specified folder
;                   $sExclude = optional: Exclude filter(s), semicolon delimited. Wildcards allowed.
;                               (Example: "Unins*" will remove all files/folders that begin with "Unins")
;                   $iRetFormat =  optional: return format
;                                  0 = one-dimensional array, 0-based
;                                  1 = one-dimensional array, 1-based (default)
;                                  2 = String ( "|" delimited)
; Requirement(s):   AutoIt Version 3.3.1.1 or newer
; Return Value(s):  on success: 1-based or 0-based array or string (dependent on $iRetFormat)
;                   If no path is found, @error and @extended are set to 1, returns empty string
;                   If no filter is found, @error and @extended are set to 2, returns empty string
;                   If $iRetFormat is invalid, @error and @extended are set to 3, returns empty string
;                   If no data is found, @error and @extended are set to 4, returns empty string
; Author(s):        Half the AutoIt Community
; ====================================================================================================
Func _FileListToArrayXT($sPath = @ScriptDir, $sFilter = "*", $iRetItemType = 0, $iRetPathType = 0, $bRecursive = False, $sExclude = "", $iRetFormat = 1)
	Local $hSearchFile, $sFile, $sFileList, $sWorkPath, $sRetPath, $iRootPathLen, $iPCount, $iFCount, $fDirFlag

	;[Check And Prepare Parameters]
	;---------------
	If $sPath = -1 OR $sPath = Default Then $sPath = @ScriptDir
	; Strip Leading/Trailing Spaces And Semi-Colons, All Adjacent Semi-Colons, And Spaces Surrounding Semi-Colons
	$sPath = StringRegExpReplace(StringRegExpReplace($sPath, "(\s*;\s*)+", ";"), "\A;|;\z", "")
	; Check That At Least One Path Is Set
	If $sPath = "" Then Return SetError(1, 1, "")
	;-----
	If $sFilter = -1 OR $sFilter = Default Then $sFilter = "*"
	; Prepare Filter
	; Strip Leading/Trailing Spaces And Semi-Colons, All Adjacent Semi-Colons, And Spaces Surrounding Semi-Colons
	$sFilter = StringRegExpReplace(StringRegExpReplace($sFilter, "(\s*;\s*)+", ";"), "\A;|;\z", "")
	; Check For Invalid Chars Or That At Least One Filter Is Set
	If StringRegExp($sFilter, "[\\/><:\|]|(?s)\A\s*\z") Then Return SetError(2, 2, "")
	If $bRecursive Then
		;Convert $sFilter for Regular Expression
		$sFilter = StringRegExpReplace($sFilter, '([\Q\.+[^]$(){}=!\E])', '\\$1')
		$sFilter = StringReplace($sFilter, "?", ".")
		$sFilter = StringReplace($sFilter, "*", ".*?")
		$sFilter = "(?i)\A(" & StringReplace($sFilter, ";", "$|") & "$)"  ; Case-Insensitive, Convert ';' To '|', Match From First Char, Terminate Strings
		;$sFilter = "(?i)\A" & StringReplace($sFilter, ";", "|") & "\z"
	EndIf
	;-----
	If $iRetItemType <> "1" AND $iRetItemType <> "2" Then $iRetItemType = "0"
	;-----
	If $iRetPathType <> "1" AND $iRetPathType <> "2" Then $iRetPathType = "0"
	;-----
	$bRecursive = ($bRecursive = "1")
	;-----
	If $sExclude = -1 OR $sExclude = Default Then $sExclude = ""
	If $sExclude Then
	  ; Prepare $sExclude
	  ; Strip Leading/Trailing Spaces And Semi-Colons, All Adjacent Semi-Colons, And Spaces Surrounding Semi-Colons
	  $sExclude = StringRegExpReplace(StringRegExpReplace($sExclude, "(\s*;\s*)+", ";"), "\A;|;\z", "")
	  ; Convert $sExclude for Regular Expression
	  $sExclude = StringRegExpReplace($sExclude, '([\Q\.+[^]$(){}=!\E])', '\\$1')
	  $sExclude = StringReplace($sExclude, "?", ".")
	  $sExclude = StringReplace($sExclude, "*", ".*?")
	  $sExclude = "(?i)\A(" & StringReplace($sExclude, ";", "$|") & "$)" ; Case-Insensitive, Convert ';' To '|', Match From First Char, Terminate Strings
	  ;$sExclude = "(?i)\A" & StringReplace($sExclude, ";", "|") & "\z"
	EndIf
	;-----
	; If $iRetFormat <> "0" And $iRetFormat <> "2" Then $iRetFormat = "1"
	If NOT ($iRetItemType = 0 OR $iRetItemType = 1 OR $iRetItemType = 2) Then Return SetError(3, 3, "")
	;---------------
	;[/Check And Prepare Parameters]

	;---------------

	Local $aPath = StringSplit($sPath, ';', 1)      ; Paths Array
	Local $aFilter = StringSplit($sFilter, ';', 1)  ; Filters Array

	;---------------

	; Different Handling For Recursion (Strategy: Unfiltered Search For All Items And Filter Unwanted)
	If $bRecursive Then
		; Different Handling Dependent On $Sexclude Parameter Is Set Or Not
		If $sExclude Then
			; Path Loop
			For $iPCount = 1 To $aPath[0]
				; Ensure Exact One Trailing Slash
				$sPath = StringRegExpReplace($aPath[$iPCount], "[\\/]+\z", "") & "\"
				If NOT FileExists($sPath) Then ContinueLoop
				$iRootPathLen = StringLen($sPath) - 1

				Local $aPathStack[1024] = [1, $sPath]

				While $aPathStack[0] > 0
					$sWorkPath = $aPathStack[$aPathStack[0]]
					$aPathStack[0] -= 1
					;-----
					$hSearchFile = FileFindFirstFile($sWorkPath & '*')
					If @Error Then
						FileClose($hSearchFile)
						ContinueLoop
					EndIf
					;-----
					Switch $iRetPathType
						; Full Path
						Case 2
							$sRetPath = $sWorkPath
						; Relative Path
						Case 1
							$sRetPath = StringTrimLeft($sWorkPath, $iRootPathLen + 1)
					EndSwitch
					;-----
					Switch $iRetItemType
						; Files Only
						Case 1
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								$fDirFlag = @Extended
								If $fDirFlag Then
									$aPathStack[0] += 1
									If UBound($aPathStack) <= $aPathStack[0] Then ReDim $aPathStack[UBound($aPathStack) * 2]
									$aPathStack[$aPathStack[0]] = $sWorkPath & $sFile & "\"
									ContinueLoop
								EndIf
								If StringRegExp($sFile, $sExclude) Then ContinueLoop
								If StringRegExp($sFile, $sFilter) Then
									$sFileList &= $sRetPath & $sFile & "|"
								EndIf
							WEnd
						; FoldeRs Only
						Case 2
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								$fDirFlag = @Extended
								If StringRegExp($sFile, $sExclude) Then ContinueLoop
								If $fDirFlag Then
									$aPathStack[0] += 1
									If UBound($aPathStack) <= $aPathStack[0] Then ReDim $aPathStack[UBound($aPathStack) * 2]
									$aPathStack[$aPathStack[0]] = $sWorkPath & $sFile & "\"
									If StringRegExp($sFile, $sFilter) Then
										$sFileList &= $sRetPath & $sFile & "|"
									EndIf
								EndIf
							WEnd
						; Files And Folders
						Case Else
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								$fDirFlag = @Extended
								If StringRegExp($sFile, $sExclude) Then ContinueLoop
								If $fDirFlag Then
									$aPathStack[0] += 1
									If UBound($aPathStack) <= $aPathStack[0] Then ReDim $aPathStack[UBound($aPathStack) * 2]
									$aPathStack[$aPathStack[0]] = $sWorkPath & $sFile & "\"
								EndIf
								If StringRegExp($sFile, $sFilter) Then
									$sFileList &= $sRetPath & $sFile & "|"
								EndIf
							WEnd
					EndSwitch
					;-----
				WEnd
				FileClose($hSearchFile)
			; $iPCount - Next Path
			Next
		; If Not $sExclude
		Else
			; Path Loop
			For $iPCount = 1 To $aPath[0]
				; Ensure Exact One Trailing Slash
				$sPath = StringRegExpReplace($aPath[$iPCount], "[\\/]+\z", "") & "\"
				If NOT FileExists($sPath) Then ContinueLoop
				$iRootPathLen = StringLen($sPath) - 1

				Local $aPathStack[1024] = [1, $sPath]

				While $aPathStack[0] > 0
					$sWorkPath = $aPathStack[$aPathStack[0]]
					$aPathStack[0] -= 1
					;-----
					$hSearchFile = FileFindFirstFile($sWorkPath & '*')
					If @Error Then
						FileClose($hSearchFile)
						ContinueLoop
					EndIf
					;-----
					Switch $iRetPathType
						; Full Path
						Case 2
							$sRetPath = $sWorkPath
						; Relative Path
						Case 1
							$sRetPath = StringTrimLeft($sWorkPath, $iRootPathLen + 1)
					EndSwitch
					;-----
					Switch $iRetItemType
						; Files Only
						Case 1
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								If @Extended Then
									$aPathStack[0] += 1
									If UBound($aPathStack) <= $aPathStack[0] Then ReDim $aPathStack[UBound($aPathStack) * 2]
									$aPathStack[$aPathStack[0]] = $sWorkPath & $sFile & "\"
									ContinueLoop
								EndIf
								If StringRegExp($sFile, $sFilter) Then
									$sFileList &= $sRetPath & $sFile & "|"
								EndIf
							WEnd
						; Folders Only
						Case 2
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								If @Extended Then
									$aPathStack[0] += 1
									If UBound($aPathStack) <= $aPathStack[0] Then ReDim $aPathStack[UBound($aPathStack) * 2]
									$aPathStack[$aPathStack[0]] = $sWorkPath & $sFile & "\"
									If StringRegExp($sFile, $sFilter) Then
										$sFileList &= $sRetPath & $sFile & "|"
									EndIf
								EndIf
							WEnd
						; Files And Folders
						Case Else
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								If @Extended Then
									$aPathStack[0] += 1
									If UBound($aPathStack) <= $aPathStack[0] Then ReDim $aPathStack[UBound($aPathStack) * 2]
									$aPathStack[$aPathStack[0]] = $sWorkPath & $sFile & "\"
								EndIf
								If StringRegExp($sFile, $sFilter) Then
									$sFileList &= $sRetPath & $sFile & "|"
								EndIf
							WEnd
					EndSwitch
					;-----
				WEnd
				FileClose($hSearchFile)
			; $iPCount - Next Path
			Next
		; If $sExclude
		EndIf
	; If Not $bRecursive (Strategy: Filtered Search For Items)
	Else
		; Different Handling Dependent On $Sexclude Parameter Is Set Or Not
		If $sExclude Then
			; Path Loop
			For $iPCount = 1 To $aPath[0]
				; Ensure Exact One Trailing Slash
				$sPath = StringRegExpReplace($aPath[$iPCount], "[\\/]+\z", "") & "\"
				If NOT FileExists($sPath) Then ContinueLoop
				;-----
				Switch $iRetPathType
					; Full Path
					Case 2
						$sRetPath = $sPath
					; Relative Path
					Case 1
						$sRetPath = ""
				EndSwitch

				; Filter Loop
				For $iFCount = 1 To $aFilter[0]
					;-----
					$hSearchFile = FileFindFirstFile($sPath & $aFilter[$iFCount])
					If @Error Then
						FileClose($hSearchFile)
						ContinueLoop
					EndIf
					;-----
					Switch $iRetItemType
						; Files Only
						Case 1
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								; Bypass Folder
								If @Extended Then ContinueLoop
								; Check For Exclude Files
								If StringRegExp($sFile, $sExclude) Then ContinueLoop
								$sFileList &= $sRetPath & $sFile & "|"
							WEnd
						; Folders Only
						Case 2
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								; Bypass File
								If @Extended Then
									; Check For Exclude Folder
									If StringRegExp($sFile, $sExclude) Then ContinueLoop
									$sFileList &= $sRetPath & $sFile & "|"
								EndIf
							WEnd
						; Files And Folders
						Case Else
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								; Check For Exclude Files/Folder
								If StringRegExp($sFile, $sExclude) Then ContinueLoop
								$sFileList &= $sRetPath & $sFile & "|"
							WEnd
					EndSwitch
					FileClose($hSearchFile)
				; $iFCount - Next Filter
				Next
			; $iPCount - Next Path
			Next
		; If Not $sExclude
		Else
			; Path Loop
			For $iPCount = 1 To $aPath[0]
				; Ensure Exact One Trailing Slash
				$sPath = StringRegExpReplace($aPath[$iPCount], "[\\/]+\z", "") & "\"
				If NOT FileExists($sPath) Then ContinueLoop
				;-----
				Switch $iRetPathType
					; Full Path
					Case 2
						$sRetPath = $sPath
					; Relative Path
					Case 1
						$sRetPath = ""
				EndSwitch

				; Filter Loop
				For $iFCount = 1 To $aFilter[0]
					;-----
					$hSearchFile = FileFindFirstFile($sPath & $aFilter[$iFCount])
					If @Error Then
						FileClose($hSearchFile)
						ContinueLoop
					EndIf
					;-----
					Switch $iRetItemType
						; Files Only
						Case 1
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								; Bypass Folder
								If @Extended Then ContinueLoop
								$sFileList &= $sRetPath & $sFile & "|"
							WEnd
						; Folders Only
						Case 2
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								; Bypass File
								If @Extended Then
									$sFileList &= $sRetPath & $sFile & "|"
								EndIf
							WEnd
						; Files And Folders
						Case Else
							While True
								$sFile = FileFindNextFile($hSearchFile)
								If @Error Then
									FileClose($hSearchFile)
									ExitLoop
								EndIf
								$sFileList &= $sRetPath & $sFile & "|"
							WEnd
					EndSwitch
					FileClose($hSearchFile)
				;$iFCount - Next Filter
				Next
			; $iPCount - Next Path
			Next
		; If $sExclude
		EndIf
	; If $bRecursive
	EndIf

	;---------------

	; Set According Return Value
	If $sFileList Then
		Switch $iRetFormat
			; Return A Delimited String
			Case 2
				Return StringTrimRight($sFileList, 1)
			; Return A 0-Based Array
			Case 0
				Return StringSplit(StringTrimRight($sFileList, 1), "|", 2)
			; Return A 1-Based Array
			Case Else
				Return StringSplit(StringTrimRight($sFileList, 1), "|", 1)
		EndSwitch
	Else
		Return SetError(4, 4, "")
	EndIf
EndFunc   ;==>_FileListToArrayX
